package com.hslxy.niukewang.controller;

import com.hslxy.niukewang.component.Page;
import com.hslxy.niukewang.entity.Event;
import com.hslxy.niukewang.entity.User;
import com.hslxy.niukewang.event.EventProducer;
import com.hslxy.niukewang.service.FollowService;
import com.hslxy.niukewang.service.UserService;
import com.hslxy.niukewang.util.CommunityUtil;
import com.hslxy.niukewang.util.Constant;
import com.hslxy.niukewang.util.HostHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
public class FollowController implements Constant{

    @Autowired
    private FollowService followService;

    @Autowired
    private HostHolder hostHolder;

    @Autowired
    private UserService userService;

    @Autowired
    private EventProducer eventProducer;

    @RequestMapping(path = "/follow",method = RequestMethod.POST)
    @ResponseBody
    public String follow(int entityType, int entityId){
        User user = hostHolder.getUsers();
        followService.follow(user.getId(), entityType, entityId);
        Event event = new Event()
                .setTopic(TOPIC_FOLLOW)
                .setUserId(hostHolder.getUsers().getId())
                .setEntityType(entityType)
                .setEntityUserId(entityId)
                .setEntityId(entityId);
        eventProducer.fireEvent(event);
        return CommunityUtil.getJSONString(0, "已关注");
    }


    @RequestMapping(path = "/unfollow",method = RequestMethod.POST)
    @ResponseBody
    public String unfollow(int entityType, int entityId){
        User user = hostHolder.getUsers();
        followService.unfollow(user.getId(), entityType, entityId);
        return CommunityUtil.getJSONString(0, "取消关注");
    }

    //    查询我关注的人
    @RequestMapping(path = "/followees/{userId}",method = RequestMethod.GET)
    public String findFollowees(@PathVariable(value = "userId") int userId, Page page, Model model){
        User userById = userService.findUserById(userId);
        if(userById == null){
            throw new RuntimeException("该用户不存在");
        }
        model.addAttribute("user", userById);
        page.setLimit(5);
        page.setPath("/followees/" + userId);
        page.setRows((int) followService.findFolloweeCount(userId, Constant.ENTITY_TYPE_USER));
        List<Map<String, Object>> followees = followService.findFollowees(userId, page.getOffset(), page.getLimit());
        if(followees != null){
            followees.forEach(stringObjectMap -> {
                User user = (User) stringObjectMap.get("user");
                stringObjectMap.put("hasFollowed", hasFollowed(user.getId()));
            });
        }
        model.addAttribute("followees", followees);
        return "/site/followee";
    }

    //    查询我被谁关注
    @RequestMapping(path = "/followers/{userId}",method = RequestMethod.GET)
    public String  findFollower(@PathVariable(value = "userId")int userId, Page page, Model model){
        User userById = userService.findUserById(userId);
        if(userById == null){
            throw new RuntimeException("该用户不存在");
        }
        model.addAttribute("user", userById);
        page.setLimit(5);
        page.setPath("/followers/" + userId);
        page.setRows((int) followService.findFollowerCount(Constant.ENTITY_TYPE_USER, userId));
        List<Map<String, Object>> followers = followService.findFollower(userId, page.getOffset(), page.getLimit());
        if(followers != null){
            followers.forEach(stringObjectMap -> {
                User user = (User) stringObjectMap.get("user");
                stringObjectMap.put("hasFollowed", hasFollowed(user.getId()));
            });
        }
        model.addAttribute("followers", followers);
        return "/site/follower";
    }
    private boolean hasFollowed(int userId){
        if(hostHolder.getUsers() == null){
            return false;
        }
        return followService.isFollowed(hostHolder.getUsers().getId(), Constant.ENTITY_TYPE_USER, userId);
    }

}
